using Geek.Server.App.Logic.WebServer;
using Geek.Server.Core.Actors;
using Geek.Server.Core.Net.Http;

namespace Server.Logic.WebServer
{
    /// <summary>
    /// 中心服 角色信息管理
    /// </summary>
    public partial class WebServerCompAgent
    {
        [Service]
        public virtual Task<HttpResponse> ExecuteRoleCreateNotify(WebRoleCreateNotify notify)
        {
            if (notify.AccountId == 0 || notify.ServerId == 0)
                return Task.FromResult(HttpResponse.Create((int)MessageErrorCode.ParametersError));

            if (notify.RoleInfo == null || notify.RoleInfo.Id == 0 || string.IsNullOrEmpty(notify.RoleInfo.Name))
                return Task.FromResult(HttpResponse.Create((int)MessageErrorCode.ParametersError));

            if (!State.AccountMap.TryGetValue(notify.AccountId, out var accountInfo))
                return Task.FromResult(HttpResponse.Create((int)MessageErrorCode.AccountNotExists));

            if (accountInfo.RoleMap.ContainsKey(notify.ServerId))
                LOGGER.Warn("帐号{0}已经在服务器{1}创建过角色, 将使用新的角色数据覆盖原数据", notify.AccountId, notify.ServerId);

            accountInfo.RoleMap[notify.ServerId] = notify.RoleInfo;
            return Task.FromResult(HttpResponse.Create());
        }

        [Service]
        public virtual Task<HttpResponse> ExecuteRoleLoginNotify(WebRoleLoginNotify notify)
        {
            if (notify.AccountId == 0 || notify.Id == 0 || notify.ServerId == 0)
                return Task.FromResult(HttpResponse.Create((int)MessageErrorCode.ParametersError));

            if (!State.AccountMap.TryGetValue(notify.AccountId, out var accountInfo))
                return Task.FromResult(HttpResponse.Create((int)MessageErrorCode.AccountNotExists));

            if (!accountInfo.RoleMap.TryGetValue(notify.ServerId, out var roleInfo) || roleInfo.Id != notify.Id)
                return Task.FromResult(HttpResponse.Create((int)MessageErrorCode.RoleNotExists));

            roleInfo.LastLoginTime = notify.Time;
            return Task.FromResult(HttpResponse.Create());
        }

        [Service]
        public virtual Task<HttpResponse> ExecuteRoleUpdateNotify(WebRoleUpdateNotify notify)
        {
            if (notify.AccountId == 0 || notify.ServerId == 0)
                return Task.FromResult(HttpResponse.Create((int)MessageErrorCode.ParametersError));

            if (notify.RoleInfo == null || notify.RoleInfo.Id == 0 || string.IsNullOrEmpty(notify.RoleInfo.Name))
                return Task.FromResult(HttpResponse.Create((int)MessageErrorCode.ParametersError));

            if (!State.AccountMap.TryGetValue(notify.AccountId, out var accountInfo))
                return Task.FromResult(HttpResponse.Create((int)MessageErrorCode.AccountNotExists));

            if (!accountInfo.RoleMap.TryGetValue(notify.ServerId, out var roleInfo) || roleInfo.Id != notify.RoleInfo.Id)
                return Task.FromResult(HttpResponse.Create((int)MessageErrorCode.RoleNotExists));

            roleInfo.Name = notify.RoleInfo.Name;
            return Task.FromResult(HttpResponse.Create());
        }

        [Service]
        public virtual Task<HttpResponse> RequestQueryAccountRole(WebQueryAccountRoleReq req)
        {
            if (req.AccountId == 0 || req.ServerId == 0)
                return Task.FromResult(HttpResponse.Create((int)MessageErrorCode.ParametersError));

            if (!State.AccountMap.TryGetValue(req.AccountId, out var accountInfo))
                return Task.FromResult(HttpResponse.Create((int)MessageErrorCode.AccountNotExists));

            if (!accountInfo.RoleMap.TryGetValue(req.ServerId, out var roleInfo))
                return Task.FromResult(HttpResponse.Create((int)MessageErrorCode.RoleNotExists));

            return Task.FromResult((HttpResponse)new WebQueryAccountRoleResp
            {
                RoleInfo = roleInfo
            });
        }
    }
}